﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;

namespace RoadTeam.FineFile
{
    public interface IGetPath
    {
        List<String> GetFileFullPath(List<String> liPath, String SourcePath);
    }


    public class GetNewFullPath : IGetPath
    {
        #region 获取新打包方式全部文件路径
        public List<String> GetFileFullPath(List<String> liPath, String SourcePath)
        {
            String FileName = String.Empty;
            String BinSourcePath = SourcePath;

            String Extension = String.Empty;

            List<String> PathFileName = new List<String>();
            String[] Exception = new string[] { "ashx", "html", "htm", "css", "js", "dll", "xml", "png", "jpg", "jpeg", "gif" };




            foreach (String StrPath in liPath)
            {

                if (String.IsNullOrEmpty(StrPath)) continue;

                FileName = FileOpera.GetFileName(StrPath);

                String[] Extensions = FileName.Split('.');
                Extension = Extensions[Extensions.Length - 1];

                if (Exception.Contains(Extension))
                {
                    PathFileName.Add(StrPath);
                    continue;
                }

                String[] fileList = Directory.GetFiles(BinSourcePath + "\\bin\\", FileName + "*");
                String virtualPath = String.Empty;
                String assembly = String.Empty;
                foreach (String FilePath in fileList)
                {
                    if (!GetDllInfo(FilePath, StrPath, ref  virtualPath, ref assembly)) continue;
                    // if (String.IsNullOrEmpty(assembly)) continue;
                    PathFileName.Add(StrPath);
                    PathFileName.Add("bin\\" + FileOpera.GetFileName(FilePath));
                    PathFileName.Add("bin\\" + assembly + ".dll");
                    PathFileName.Add("bin\\" + assembly + ".pdb");

                }
            }
            return PathFileName;
        }

        private bool GetDllInfo(string FilePath, String StrPath, ref String virtualPath, ref String assembly)
        {
            LoadXML(FilePath, ref virtualPath, ref assembly);

            virtualPath = virtualPath.Substring(virtualPath.IndexOf('/', 2) + 1, virtualPath.Length - virtualPath.IndexOf('/', 2) - 1);
            virtualPath = virtualPath.Replace('/', '\\');
            return virtualPath == StrPath;
        }

        private void LoadXML(string FilePath, ref String virtualPath, ref String assembly)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(FilePath);
            XmlNode xlm = doc.SelectSingleNode("preserve");

            if (xlm.Attributes["assembly"] != null)
                assembly = xlm.Attributes["assembly"].Value.ToString();

            if (xlm.Attributes["virtualPath"] != null)
                virtualPath = xlm.Attributes["virtualPath"].Value.ToString();
        }


        #endregion
    }





    public class GetOldFullPath : IGetPath
    {
        public List<String> GetFileFullPath(List<String> liPath, String SourcePath)
        {
            String FileName = String.Empty;
            String FileFullPath = String.Empty;
            String BinSourcePath = SourcePath;

            String Extension = String.Empty;

            List<String> PathFileName = new List<String>();
            String[] Exception = new string[] { "ashx", "html", "htm", "css", "js", "dll", "xml", "png", "jpg", "jpeg", "gif" };


            String[] NotException = new string[] { "asax" };

            foreach (String StrPath in liPath)
            {

                if (String.IsNullOrEmpty(StrPath)) continue;

                FileName = FileOpera.GetFileName(StrPath);

                String[] Extensions = FileName.Split('.');
                Extension = Extensions[Extensions.Length - 1];

                if (Exception.Contains(Extension))
                {
                    PathFileName.Add(StrPath);
                    continue;
                }
                if (NotException.Contains(Extension)) continue;

                FileFullPath = SourcePath + "\\" + StrPath;
                PathFileName.Add(StrPath);
                PathFileName.Add("bin\\" + GetDllPath(FileFullPath) + ".dll");

            }
            return PathFileName;
        }

        public String GetDllPath(String FileFullPath)
        {
            String StrFirstLine = GetFirstLine(FileFullPath);
            if (String.IsNullOrEmpty(StrFirstLine)) return String.Empty;

            int index = StrFirstLine.IndexOf("inherits") + 10;

            String NewStr = String.Empty;
            Stack<char> sta = new Stack<char>();
            sta.Push('"');
            for (int i = index; i < StrFirstLine.Length; i++)
            {
                if ('"' != StrFirstLine[i])
                    NewStr += StrFirstLine[i].ToString();
                else
                    sta.Pop();
                if (sta.Count == 0) break;
            }
            return NewStr.Split(',')[1].Trim();
        }

        private String GetFirstLine(String FileFullPath)
        {
            String StrFirstLine = String.Empty;
            FileStream fils = new FileStream(FileFullPath, FileMode.Open);
            StreamReader sdr = new StreamReader(fils);
            StrFirstLine = sdr.ReadLine();
            sdr.Dispose();
            return StrFirstLine;
        }



    }


}
